package com.mzy.datepackage.M2022.July.one;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/7/1 9:55
 * @Version 1.0
 */
@Slf4j
public class demo {

    private static final String filePath = "F:\\每日脚本暂存\\新建文件路径\\2022.06.29\\1.txt";

    public static void main(String[] args) throws IOException {
        byte[] xes = getContent("X");
        System.out.println(xes);
    }

    /**
     * <p>Title: getContent</p>
     * <p>Description:根据文件路径读取文件转出byte[] </p>
     *
     * @param filePath文件路径
     * @return 字节数组
     * @throws IOException
     */
    public static byte[] getContent(String xx) throws IOException {
        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            log.info("file too big...");
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }
        // 确保所有数据均被读取
        if (offset != buffer.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        fi.close();
        return buffer;
    }
}
